<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of taskObject
 *
 * @author nathaniel.stevens.ad
 */
require_once 'model/taskModelClass.php';

class taskControllerClass extends commonController {

    public function __construct($model) {
        $this->model = $model;
    }

    //ACTIONS*******************************************************************
    //****VIEWS*****************************************************************
    public function viewTaskDetails() {
        $this->model->string = $this->model->getTaskDetailsPage();
    }

    //****FORMS*****************************************************************

    function editExistingTask() {
        $this->model->string = $this->model->getEditTaskPage();
    }

    function proposeNewSubtask() {
        $this->model->string = $this->model->getNewSubtaskPage();
    }

    //****PROCESSING************************************************************

    function processNewSubtask() {
        //echo "process new subtask for task: ";
        //echo $this->taskModel->ID;
        $url = $this->model->processNewSubtaskRequestQuarterly();
        if ($url) {
            echo header($url);
        } else {
            echo "error processing new subtask";
        }
    }

    function processExistingTaskRequest() {
        if (isset($_POST['submit'])) {
            if (strcmp("Edit Task", $_POST['submit']) === 0) {
                $this->editExistingTask();
            } 
            else if (strcmp("Delete Task", $_POST['submit']) === 0) {
                $this->processDeleteTask();
            }
            else if (strcmp("Deactivate", $_POST['submit']) === 0) {
                $this->processDeactivateTask();
            } 
            else if (strcmp("Activate", $_POST['submit']) === 0) {
                $this->processActivateTask();
            } 
            else if (strcmp("Add Notes", $_POST['submit']) === 0) {
                $this->processAddNotesRequest();
            }
            else {
                $this->processChangeTaskStatus();
            }
        }
    }
    
    private function processDeleteTask() {
        if (sizeof($this->model->subtasks) > 0) {
            $url = $_SERVER['HTTP_REFERER'];
            $url .= "&update=Cannot delete task delete subtask(s) first";
            echo header('location:'.$url);
        } else {
            $url = $this->model->deleteTask();
            if($url){
                echo header($url);
            }
            else{
                echo "Error deleting task";
            }
        }
    }

    private function processDeactivateTask() {
        echo 'deactivate task ';
        echo $this->model->ID;
    }

    private function processActivateTask() {
        echo 'activate task ';
        echo $this->model->ID;
    }

    private function processChangeTaskStatus() {
        $url = $this->model->changeTaskStatus();
        if ($url) {
            echo header($url);
        } else {
            echo "Error changing task status";
        }
    }

    public function toString() {
        $output = "";
        $output .= "WBS: $this->wbsString Title: $this->title Start: $this->startDate End: $this->endDate Status: $this->status ";
        $output .= "Duration: $this->duration Desc: $this->description PCost: $this->proposedCost <br>";
        if ($this->subtasks) {
            for ($i = 0; $i < sizeof($this->subtasks); $i++) {
                $curSubtask = $this->subtasks[$i];
                $output .= "$curSubtask->title <br>";
                $output .= $curSubtask->financialObject->toString();
            }
        }
        return $output;
    }

}

?>
